// pages/sales/word_art/word_art.js
var cf = require("../../../config.js");
var util = require("../../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var baseHandle = require("../../template/baseHandle.js");
var app = getApp();
Page(Object.assign({}, baseHandle,{

  /**
   * 页面的初始数据
   */
  data: {
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    app: app,
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    wordArtGroupList: [],
    wordArtGroupIdx: -1,//当前分组的idx
    wordArtList: [],
    isLoading: false,
    pageSize: 15,
    curPage: 1,
    total:0,
    isLoading:false,
    isBottom: false,

    isNeedReload:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var ctx = this;
    wx.hideShareMenu();
    app.getUserInfo(this, options, function (userInfo, res) {
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.setData({
        eId: wx.getStorageSync('employee').id
      })
      ctx.findGroup();
      ctx.fetchRecentOrCustWordArt();
    });
  },
  
  
  changeGroup(e){
    var idx = e.currentTarget.dataset.gidx;
    var ctx = this;
    ctx.setData({
      wordArtGroupIdx: idx,
      wordArtList:[],
      isBottom:false,
      curPage:1,
      isLoading:false,
      total:0,
    });
    if (-1 == idx){//最近使用的 话术
      ctx.fetchRecentOrCustWordArt();
    }else if(-2 == idx){//自定义话术分组
      ctx.fetchRecentOrCustWordArt(true);
    }else{//分组查询
      ctx.fetchWordArtByGroup();
    }
  },
  toEditWordArt(e){
    var id = e.currentTarget.dataset.id;
    var iscust = e.currentTarget.dataset.iscust;
    wx.navigateTo({
      url: './word_art_detail?id=' + id + "&iscust=" + iscust,
    })
  },
  toAddWordArt(){
    wx.navigateTo({
      url: './word_art_detail',
    })
  },
  srcrollBotWAGHandle(){
    console.log("wag");
  },
  srcrollBotHandle(e){
    var ctx = this;
    var idx = ctx.data.wordArtGroupIdx;
    if (ctx.data.isLoading || ctx.data.isBottom ){
      return;
    }
    ctx.setData({
      curPage: ++ctx.data.curPage
    });
    if (-1 == idx) {//最近使用的 话术
      ctx.fetchRecentOrCustWordArt();
    } else if (-2 == idx) {//自定义话术分组
      ctx.fetchRecentOrCustWordArt(true);
    } else {//分组查询
      ctx.fetchWordArtByGroup();
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var ctx = this;
    if (ctx.data.isNeedReload){
      ctx.setData({
        isNeedReload: false,
        wordArtList: [],
        isBottom: false,
        curPage: 1,
        isLoading: false,
        total: 0,
      });
      if (-1 == ctx.data.wordArtGroupIdx) {//最近使用的 话术
        ctx.fetchRecentOrCustWordArt();
      } else if (-2 == ctx.data.wordArtGroupIdx) {//自定义话术分组
        ctx.fetchRecentOrCustWordArt(true);
      } else {//分组查询
        ctx.fetchWordArtByGroup();
      }
    }else{
      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("1111")
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
}))